// src/utils/InteractionController.js
import * as THREE from "three";

export class InteractionController {
  constructor(renderer, camera, model, modelManager) {
    this.renderer = renderer;
    this.camera = camera;
    this.model = model;
    this.modelManager = modelManager;
    this.raycaster = new THREE.Raycaster();
    this.tooltipCallback = null;
    this.partNames = modelManager.getPartNames();
  }

  setTooltipCallback(callback) {
    this.tooltipCallback = callback;
  }

  onMouseMove = (event) => {
    if (!this.model || !this.raycaster || !this.camera) return;

    const rect = this.renderer.domElement.getBoundingClientRect();
    const mouse = new THREE.Vector2();
    mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
    mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;

    this.raycaster.setFromCamera(mouse, this.camera);
    const intersects = this.raycaster.intersectObject(this.model, true);

    if (intersects.length > 0) {
      const object = intersects[0].object;
      let partName = "";
      let currentObj = object;
      let matchedPart = null;

      // 获取部件名称列表
      const partNames = this.modelManager.getPartNames();

      while (currentObj && !matchedPart) {
        if (currentObj.name) {
          matchedPart = partNames.find((name) =>
            currentObj.name.toLowerCase().includes(name.toLowerCase())
          );

          if (matchedPart) {
            partName = matchedPart;
          }
        }
        currentObj = currentObj.parent;
      }

      if (!matchedPart && object.name) {
        partName = object.name;
        const tooltipData = {
          name: partName,
          params: {
            名称: object.name,
            类型: object.type,
            材质: object.material
              ? object.material.name || object.material.type
              : "无",
          },
          position: {
            x: event.clientX + 15,
            y: event.clientY - 10
          }
        };

        if (this.tooltipCallback) {
          this.tooltipCallback({
            visible: true,
            name: tooltipData.name,
            params: tooltipData.params,
            position: tooltipData.position
          });
        }
      } else if (matchedPart) {
        const tooltipData = {
          name: partName,
          params: {
            名称: object.name,
            类型: object.type,
            材质: object.material
              ? object.material.name || object.material.type
              : "无",
          },
          position: {
            x: event.clientX + 15,
            y: event.clientY - 10
          }
        };

        if (this.tooltipCallback) {
          this.tooltipCallback({
            visible: true,
            name: tooltipData.name,
            params: tooltipData.params,
            position: tooltipData.position
          });
        }
      }
    } else if (this.tooltipCallback) {
      this.tooltipCallback({ visible: false });
    }
  }

  dispose() {
    // 清理事件监听器等
  }
}